package com.hous.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

/**
* @author [houshuai@asiainfo.com]
* @version [版本号,2017年4月19日] 
* @see [相关类/方法]
* @since [产品/模块版本]
* @description TODO
*/
@Controller
@RequestMapping("/download")
public class DownloadController
{

	@RequestMapping("/")
	public void download(HttpServletRequest request, HttpServletResponse response) throws Exception {
		FileInputStream fs=new FileInputStream("D:\\02Temp\\test.xlsx");  //获取d://test.xls  
        Workbook wb= WorkbookFactory.create(fs);    
        Sheet sheet=wb.getSheetAt(0);  //获取到工作表，因为一个excel可能有多个工作表  
        Row row=sheet.getRow(0);  //获取第一行（excel中的行默认从0开始，所以这就是为什么，一个excel必须有字段列头），即，字段列头，便于赋值  
        
//        FileOutputStream out=new FileOutputStream("D:\\02Temp\\test2.xlsx");  //向d://test.xls中写数据  
        
        row=sheet.createRow((short)(sheet.getLastRowNum()+1)); //在现有行号后追加数据  
        row.createCell(0).setCellValue("leilei"); //设置第一个（从0开始）单元格的数据  
        row.createCell(1).setCellValue(24); //设置第二个（从0开始）单元格的数据  
  
        String downloadName = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".xlsx";
        //设置MIME类型为excel
        response.setContentType("application/vnd.ms-excel");
        response.setHeader("Content-Disposition", "attachment;filename="+downloadName);
        ServletOutputStream out = response.getOutputStream();
        
        wb.write(out);    
        out.close();    
        out.flush();  
	}
}
